package health.lm.com.shop.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.SearchView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLChatAdapter;
import health.lm.com.shop.adapter.HLChatHistoryAdapter;
import health.lm.com.shop.entity.HLChatEntity;
import health.lm.com.shop.presenter.HLChatPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/9 11:21
 * Introduce：私信记录
 */
@Route(path = ARouterConstant.HLChatRecordActivity)
public class HLChatRecordActivity extends XListActivity<HLChatPresenter> implements HLChatView {
    @Autowired
    public String title;
    @Autowired
    public String id;
    @BindView(R2.id.titlebar)
    CommonTitleBar mTitlebar;
    @BindView(R2.id.searchView)
    SearchView mSearchView;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout mSrlLayout;
    private HLChatHistoryAdapter mChatAdapter;

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        getP().getHistoryData(id,page, pageSize);
    }

    @Override
    public int getLayoutId() {
        return R.layout.hl_activity_people;
    }

    @Override
    public HLChatPresenter newP() {
        return new HLChatPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mSearchView.setVisibility(View.GONE);
        if (TextUtils.isEmpty(title)) title = "私信记录";
        mTitlebar.getCenterTextView().setText("私信记录");
        mTitlebar.setListener((v, action, extra) -> finish());

        mChatAdapter = new HLChatHistoryAdapter(new ArrayList<>());
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        mRvList.setAdapter(mChatAdapter);

        super.recyclerView = mRvList;
        super.rlRefreshLayout = mSrlLayout;
        super.adapter = mChatAdapter;
        super.initData(savedInstanceState);
    }

    @Override
    public void setNewData(List<HLChatEntity> pNewData) {
        if (isRefresh) {
            mSrlLayout.finishRefresh();
            mChatAdapter.setNewData(pNewData);
        } else {
            mChatAdapter.addData(pNewData);
            if (pNewData.size() >= pageSize) mChatAdapter.loadMoreComplete();
            else mChatAdapter.loadMoreEnd();
        }
        //滑动到最底部
        mRvList.scrollToPosition(mChatAdapter.getItemCount() - 1);
    }
}
